home *** CD-ROM | disk | FTP | other *** search
- #include "osMacIncludes.h"
- #include "Jetsons.h"
- #include "Molecule.h"
- #include "osGraphics.h"
- #include "osMain.h"
- #include "osWindows.h"
- #include "Rotation.h"
- #include "User.h"
- #include "Windows.h"
- #include "Dispatch.h"
-
- extern PrefsT gPrefs;
-
- /* Start fun stuff */
-
- /* Called from AutoRotate, but with Option-Key held down.
- These functions are called _after_ render, but before the screen blit.
- */
-
- void GameInit(GameState *game)
- {
- game->state = STARTUP;
- }
-
- void GetGameRect(WindowPtr theWindow, Rect *gameRect)
- {
- GetMoleculeRect(theWindow, gameRect, TRUE);
- ZeroRect(gameRect);
- }
-
- void GameStartup(GameState *game, Rect *gameRect, WindowPtr theWindow)
- {
- DocInfoHand infoHand;
- int i,sum;
- unsigned char lightest,darkest; /* pix value */
- int max = -INT_MAX;
- int min = INT_MAX;
- unsigned char *rmap,*gmap,*bmap;
-
- infoHand = (DocInfoHand) osGetWRefCon(theWindow);
-
- game->theWindow = theWindow;
-
- game->paddx = gameRect->right / 2;
- game->paddy = gameRect->bottom;
-
- game->ballx = game->paddx;
- game->bally = (game->paddy - (PADDSIZE/2)) - (BALLSIZE/2);
- game->balldx = 0;
- game->balldy = -3;
-
- game->score = 0;
-
- game->state = PLAYING;
-
- /* Get Colors */
- rmap = (**infoHand).canvas.rmap;
- gmap = (**infoHand).canvas.gmap;
- bmap = (**infoHand).canvas.bmap;
-
- for (i = CTABLEFIRST; i <= CTABLELAST; i++) {
- sum = rmap[i] + gmap[i] + bmap[i];
- if (sum > max) {
- max = sum;
- lightest = i;
- }
- else if (sum < min) {
- min = sum;
- darkest = i;
- }
- }
-
- game->color = lightest;
-
- }
-
- void DrawGame(GameState *game, Rect *gameRect)
- {
- DocInfoHand infoHand;
- int rowbytes;
- unsigned char *pix, *start, *ball;
- int r,c;
- Rect paddRect;
- #define dWIDTH 7
- #define dHEIGHT 7
- unsigned char ds[dWIDTH * dHEIGHT] = { /* auto check size */
- 0xFF,0xFF,0x01,0x01,0x01,0xFF,0xFF,
- 0xFF,0x01,0x01,0x01,0x01,0x01,0xFF,
- 0x01,0x01,0x01,0x01,0x01,0x01,0x01,
- 0x01,0x01,0x01,0x01,0x01,0x01,0x01,
- 0x01,0x01,0x01,0x01,0x01,0x01,0x01,
- 0xFF,0x01,0x01,0x01,0x01,0x01,0xFF,
- 0xFF,0xFF,0x01,0x01,0x01,0xFF,0xFF
- };
-
- infoHand = (DocInfoHand) osGetWRefCon(game -> theWindow);
- rowbytes = (**infoHand).canvas.srow;
- pix = (unsigned char *) (**infoHand).pixels;
-
- /* Draw paddle row 1 */
-
- paddRect.left = game->paddx - PADDSIZEHALF;
- paddRect.right = game->paddx + PADDSIZEHALF;
- paddRect.top = game->paddy - PADDSIZEHALF;
- paddRect.bottom = game->paddy + PADDSIZEHALF;
- SectRect(&paddRect, gameRect, &paddRect);
-
- start = pix + (rowbytes * paddRect.top);
- for (r = paddRect.top; r < paddRect.bottom; r++) {
- for (c = paddRect.left; c < paddRect.right; c++) {
- *(start + c) = game->color;
- }
- start += rowbytes;
- }
-
- /* Draw ball */
- ball = ds;
- start = pix + (rowbytes * (game->bally - BALLSIZEHALF));
- for (r = 0; r < BALLSIZE; r++) {
- for (c = game->ballx - BALLSIZEHALF; c <= game->ballx + BALLSIZEHALF; c++) {
- if (*ball == 0x01) {
- *(start + c) = game->color;
- }
- ball += 1;
- }
- start += rowbytes;
- }
-
- }
-
- void MovePaddleClockwise(GameState *game, Rect * gameRect)
- {
- if (game->paddx == gameRect->left) {
- if (game->paddy == gameRect->top) {
- game->paddx += DISTANCE;
- }
- else {
- game->paddy -= DISTANCE;
- }
- }
- else if (game->paddy == gameRect->top) {
- if (game->paddx == gameRect->right) {
- game->paddy += DISTANCE;
- }
- else {
- game->paddx += DISTANCE;
- }
- }
- else if (game->paddx == gameRect->right) {
- if (game->paddy == gameRect->bottom) {
- game->paddx -= DISTANCE;
- }
- else {
- game->paddy += DISTANCE;
- }
- }
- else if (game->paddy == gameRect->bottom) {
- if (game->paddx == gameRect->left) {
- game->paddy -= DISTANCE;
- }
- else {
- game->paddx -= DISTANCE;
- }
- }
-
- }
-
- void MovePaddleCounterClockwise(GameState *game, Rect * gameRect)
- {
- if (game->paddx == gameRect->left) {
- if (game->paddy == gameRect->bottom) {
- game->paddx += DISTANCE;
- }
- else {
- game->paddy += DISTANCE;
- }
- }
- else if (game->paddy == gameRect->top) {
- if (game->paddx == gameRect->left) {
- game->paddy += DISTANCE;
- }
- else {
- game->paddx -= DISTANCE;
- }
- }
- else if (game->paddx == gameRect->right) {
- if (game->paddy == gameRect->top) {
- game->paddx -= DISTANCE;
- }
- else {
- game->paddy -= DISTANCE;
- }
- }
- else if (game->paddy == gameRect->bottom) {
- if (game->paddx == gameRect->right) {
- game->paddy -= DISTANCE;
- }
- else {
- game->paddx += DISTANCE;
- }
- }
-
- }
-
- /*
- void DetermineKey(void);
- void DetermineKey(void)
- {
- long option,i;
- unsigned char km[16];
-
- Delay(120,&option);
-
- GetKeys((UInt32 *) km);
-
- for (i = 0; i < 128; i++) {
- option = ( ( km[i>>3] >> (i & 7) ) & 1);
- if (option) {
- Str255 s;
- NumToString(option,s);
- }
- }
-
- }
- */
-
-
-
- long KeyIsDown(int KeyNum)
- {
- long down;
- unsigned char km[16];
-
- GetKeys((UInt32 *) km);
-
- down = ( ( km[KeyNum>>3] >> (KeyNum & 7) ) & 1);
-
- return down;
- }
-
- /*
- if (CLOCKWISE != direction) {
- speed = 1;
- direction = CLOCKWISE;
- }
- else {
- }
- */
- static int speed = 1;
- static int direction = 0;
- static int slowdown = 1;
-
-
- void GamePlaying(GameState *game, Rect *gameRect)
- {
- int i;
- DocInfoHand infoHand;
- infoHand = (DocInfoHand) osGetWRefCon(game -> theWindow);
-
- /* DIRECTION */
-
- switch (direction) {
- case COUNTERCLOCKWISE:
-
- if (KeyIsDown(RIGHT_ARROW)) {
- if (KeyIsDown(LEFT_ARROW)) {
- speed -= slowdown;
- slowdown++;
- if (speed <= 0) {
- speed = 1;
- slowdown = 1;
- }
- }
- else {
- speed += 1;
- slowdown = 1;
- }
- }
- else if (KeyIsDown(LEFT_ARROW)) {
- direction = CLOCKWISE;
- speed = 1;
- slowdown = 1;
- }
- else {
- speed = 0;
- slowdown = 1;
- direction = STATIONARY;
- }
- break;
-
- case CLOCKWISE:
- if (KeyIsDown(LEFT_ARROW)) {
- if (KeyIsDown(RIGHT_ARROW)) {
- speed -= slowdown;
- slowdown++;
- if (speed <= 0) {
- speed = 1;
- slowdown = 1;
- }
- }
- else {
- speed += 1;
- slowdown = 1;
- }
- }
- else if (KeyIsDown(RIGHT_ARROW)) {
- direction = COUNTERCLOCKWISE;
- speed = 1;
- slowdown = 1;
- }
- else {
- speed = 0;
- slowdown = 1;
- direction = STATIONARY;
- }
- break;
-
- case STATIONARY:
- if (KeyIsDown(LEFT_ARROW)) {
- direction = CLOCKWISE;
- speed = 1;
- slowdown = 1;
- }
- else if (KeyIsDown(RIGHT_ARROW)) {
- direction = COUNTERCLOCKWISE;
- speed = 1;
- slowdown = 1;
- }
- break;
- }
-
- for (i = 0; i < speed; i ++) {
- if (direction == COUNTERCLOCKWISE) {
- MovePaddleCounterClockwise(game, gameRect);
- }
- else if (direction == CLOCKWISE) {
- MovePaddleClockwise(game, gameRect);
- }
- }
-
- /* BALL */
-
- {
- static int rise[] = { -4,-3,-2,-1, 0, 1, 2, 3, 4 };
- Rect ball,paddRect,dud,inset;
- Point bp;
- molecule *m = (**infoHand).molecule;
-
- game->ballx += game->balldx;
- game->bally += game->balldy;
-
- /* detect collisions */
- ball.left = game->ballx - BALLSIZEHALF;
- ball.right = game->ballx + BALLSIZEHALF;
- ball.top = game->bally - BALLSIZEHALF;
- ball.bottom = game->bally + BALLSIZEHALF;
-
- paddRect.left = game->paddx - PADDSIZEHALF;
- paddRect.right = game->paddx + PADDSIZEHALF;
- paddRect.top = game->paddy - PADDSIZEHALF;
- paddRect.bottom = game->paddy + PADDSIZEHALF;
-
- bp.h = game->ballx;
- bp.v = game->bally;
- inset = *gameRect;
- InsetRect(&inset,PADDSIZEHALF-2,PADDSIZEHALF-2);
-
- /* test 1, out of bounds */
- if (SectRect(&ball,&paddRect,&dud)) {
- /* In Paddle */
- int distance;
- distance = sqrt(
- ((game->paddx - game->ballx) * (game->paddx - game->ballx)) +
- ((game->paddy - game->bally) * (game->paddy - game->bally)) );
- distance %= 5;
- if (distance == 0) {
- distance = 1;
- }
-
- if (game->paddx == gameRect->left) {
- game->balldx = +distance;
- game->balldy = (Random() % 5);
- }
- else if (game->paddx == gameRect->right) {
- game->balldx = -distance;
- game->balldy = (Random() % 5);
- }
- else if (game->paddy == gameRect->top) {
- game->balldy = +distance;
- game->balldx = (Random() % 5);
- }
- else if (game->paddy == gameRect->bottom) {
- game->balldy = -distance;
- game->balldx = (Random() % 5);
- }
-
- game->ballx += game->balldx;
- game->bally += game->balldy;
- }
- else if (!PtInRect(bp,&inset)) {
- /* Out of bounds */
- MoleculeSunRise();
- GameStartup(game, gameRect, game->theWindow);
- }
- else {
- int rowbytes;
- unsigned char *pix;
- molecule *m;
-
- rowbytes = (**infoHand).canvas.srow;
- pix = (unsigned char *) (**infoHand).pixels;
- m = (**infoHand).molecule;
-
- if (*(pix + (game->bally * rowbytes) + game->ballx) != CTABLEBKGND) {
- /* not background color */
-
- atom *a;
- int err;
- char *aexp;
-
- err = pick_atom(m, bp.h, bp.v,&a);
- if (!err && a != NIL) {
- aexp = atom_name(a);
- aexp = ConvertExpression(aexp,ATOMEXP);
-
- kt_DeselectAllAtoms(m);
- kt_SelectOnscreenAtoms(m, aexp, SEL_SET | SEL_ONSCREEN);
- kt_HideSelection(m);
-
- #ifndef MACMOLECULE_LITE
- {
- char mess[255];
- int atms;
- select_atoms(m, NULL, SEL_ALL | SEL_ONSCREEN);
- atms = select_size(m, DO_ATOMS);
- select_atoms(m, NULL, SEL_CLEAR);
-
- if (atms == 0) {
- sprintf(mess,"You win! Now get back to work! (Click to continue)");
- game->state = OVER;
- }
- else {
- sprintf(mess,"Atoms remaining: %d",atms);
- }
- DrawTextMessage(game->theWindow, mess);
- }
- #endif
-
- do {
- game->balldx = (Random() % 5);
- game->balldy = (Random() % 5);
- } while (game->balldx == 0 && game->balldy == 0);
- }
- }
- }
-
- DrawGame(game, gameRect);
-
- }
-
- }
-
-
- void GameMain(WindowPtr theWindow)
- {
- DocInfoHand infoHand;
- static GameState game;
- static int inited = 0;
- Rect gameRect;
-
- if (!IsMolWindow(theWindow))
- return;
-
- infoHand = (DocInfoHand) osGetWRefCon(theWindow);
-
- if (!inited) {
- GameInit(&game);
- inited = 1;
- }
-
- StartUsingPixMap(infoHand,left);
-
- GetGameRect(theWindow, &gameRect);
-
- switch (game.state) {
- case STARTUP:
- GameStartup(&game,&gameRect,theWindow);
- break;
-
- case PLAYING:
- GamePlaying(&game,&gameRect);
- break;
-
- case PAUSED:
- break;
-
- case OVER:
- break;
- }
-
- StopUsingPixMap(infoHand);
-
- }
-
-
- /* called from SetLightSource, but with Option-Key held down */
- void MoleculeSunRise(void)
- {
- WindowPtr theWindow;
- DocInfoHand infoHand;
- molecule *m;
- int orig_doly;
- int doly;
-
- theWindow = osFrontWindow();
- if (!IsMolWindow(theWindow))
- return;
-
- infoHand = (DocInfoHand) osGetWRefCon(theWindow);
- m = (**infoHand).molecule;
-
- /*****/
-
- orig_doly = (**infoHand).doly;
- for (doly = orig_doly - 6;
- !(orig_doly - 5 < doly && doly < orig_doly + 5);
- doly -= 5) {
- if (osUserEventAvailable()) {
- break;
- }
-
- if (doly < -180) {
- doly = 180;
- }
-
- (**infoHand).doly = doly;
-
- SetMoleculeViewType(infoHand);
- RenderMolecule(infoHand);
-
- DrawMoleculeProper(theWindow);
-
- {
- long dud;
- osDelay(1,&dud);
- }
- }
-
- (**infoHand).doly = orig_doly;
-
- SetMoleculeViewType(infoHand);
- RenderMolecule(infoHand);
-
- DrawMoleculeProper(theWindow);
- }
-
- /* End fun stuff */
-